package io.jt808.proto.cmd;

import io.norland.buffer.AutoBuffer;
import io.norland.annotations.Proto;
import io.norland.proto.IProto;
import io.jt808.util.Tools;
import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 设置多边形区域
 */
@Proto("8604")
@Data
public class JT8604 implements IProto {
    /**
     * 区域ID
     */

    private int areaId;
    /**
     * 区域属性
     */

    private short areaProperty;
    /**
     * 起始时间,YY-MM-DD-hh-mm-ss，若区域属性0位为0则没有该字段
     */
    private Date startTime;
    /**
     * 结束时间,YY-MM-DD-hh-mm-ss，若区域属性0位为0则没有该字段
     */
    private Date endTime;
    /**
     * 最高速度,Km/h，若区域属性1位为0则没有该字段
     */
    private short maxSpeed;
    /**
     * 超速持续时间,单位秒(s),若区域属性1位为0则没有该字段
     */
    private short overSpeedLastTime;

    /**
     * 区域总顶点数
     */
    private short areaNodesCount;

    /**
     * 顶点项
     */
    private List<PolygonNodeItem> nodes;

    public final byte[] writeToBytes() {
        AutoBuffer buffer = new AutoBuffer();
        buffer.writeInt(areaId);
        buffer.writeShort(areaProperty);
        buffer.writeBcdString(Tools.getBcdDateTime(startTime), 6);
        buffer.writeBcdString(Tools.getBcdDateTime(endTime), 6);
        if ((areaProperty & 1 << 1) != 0) {
            buffer.writeShort(maxSpeed);
            buffer.writeByte(overSpeedLastTime);
        }
        buffer.writeShort(areaNodesCount);
        for (PolygonNodeItem pi : nodes) {
            buffer.writeInt((int) (pi.latitude * 1000000.0));
            buffer.writeInt((int) (pi.longitude * 1000000.0));
        }
        return buffer.array();
    }

    public final void readFromBuf(AutoBuffer buffer) {
        areaId = buffer.readInt();
        areaProperty = buffer.readShort();
        startTime = Tools.getDateTime(buffer.readBcd(), buffer.readBcd(), buffer.readBcd(),
                buffer.readBcd(), buffer.readBcd(), buffer.readBcd());
        endTime = Tools.getDateTime(buffer.readBcd(), buffer.readBcd(), buffer.readBcd(),
                buffer.readBcd(), buffer.readBcd(), buffer.readBcd());
        if ((areaProperty & 1 << 1) != 0) {
            maxSpeed = buffer.readShort();
            overSpeedLastTime = buffer.readUnsignedByte();
        }
        areaNodesCount = buffer.readShort();
        nodes = new ArrayList<>();
        while (buffer.isReadable()) {
            PolygonNodeItem pi = new PolygonNodeItem();
            pi.latitude = buffer.readInt() / 1000000.0;
            pi.longitude = buffer.readInt() / 1000000.0;
            nodes.add(pi);
        }
    }

    public class PolygonNodeItem {
        private double latitude;
        private double longitude;
    }

//    public static void main(String[] args) {
//        byte[] bytes = new byte[]{
//                0x00, 0x00, 0x00, 0x01,
//                (byte) 0xFF, (byte) 0xFF,
//                0x18, 0x10, 0x26, 0x17, 0x15, 0x13,
//                0x18, 0x10, 0x30, 0x17, 0x15, 0x13,
//                (byte) 0xa0, 0x00,
//                (byte) 0xa0,
//                0x00, 0x02,
//                0x00, 0x01, (byte) 0x86, (byte) 0xa0,
//                0x00, 0x01, (byte) 0x86, (byte) 0xa0,
//                0x00, 0x01, (byte) 0x86, (byte) 0xa0,
//                0x00, 0x01, (byte) 0x86, (byte) 0xa0,
//        };
//        JT8604 jt8600 = new JT8604();
//        jt8600.readFromBuf(new AutoBuffer(bytes));
//        System.out.println(new String(bytes).equals(new String(jt8600.writeToBytes())));
//    }
}